<?php


namespace app\admin\controller\admin;

use app\common\controller\Backstage;
use hg\apidoc\annotation as Apidoc;
use thans\jwt\facade\JWTAuth;
use think\facade\Db;

/**
 * @Apidoc\Title("管理员操作日志")
 */
class OperationLog extends Backstage
{
    /**
     * @Apidoc\Title("操作日志列表")
     * @Apidoc\Url("/admin/admin.OperationLog/listApi")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("is_excel", type="int", desc="传1导出表格")
     * @Apidoc\Param("keyword", type="string", desc="关键字")
     * @Apidoc\Param("keyword_time", type="array", desc="数组0开始 1结束")
     * @Apidoc\Param("page", type="int", desc="第几页默认第1页")
     * @Apidoc\Param("page_size", type="int", desc="每页几条默认10条")
     * @Apidoc\Returned("data", type="array", desc="返回说明")
     * @Apidoc\Returned("total", type="int", desc="总条数")
     */
    public function listApi()
    {
        $param = $this->request->param();
        $page_size = empty($param['page_size']) ? 40 : $param['page_size'];
        //条件
        $where = [];
        //模糊查询
        if (!empty($param['keyword'])) {
            $where[] = ['ol.remarks', 'like', "%{$param['keyword']}%"];
        }

        //时间查询
        if (!empty($param['keyword_time'])) {
            $time = 'ol.create_time';//查询时间字段
            $timeWhere = timeWhere($param['keyword_time'][0], $param['keyword_time'][1]);
            $where[] = [$time, '>=', $timeWhere[0]];
            $where[] = [$time, '<=', $timeWhere[1] + 86399];
        }
        //排序
        $order = ['ol.id' => 'DESC'];
        if (!empty($param['order']) && !empty($param['order_name'])) {
            $order_name = "ol.".$param['order_name'];
            switch ($param['order']) {
                case 1:
                    $order =[$order_name => 'DESC'];
                    break;
                case 2:
                    $order = [$order_name => 'ASC'];
                    break;
                default:
                    $order = ['ol.id' => 'DESC'];
            }
        }
        $list_ = (new \app\common\model\OperationLog())
            ->alias("ol")
            ->join("admin a ", " a.id = ol.admin_id", 'left')
            ->where($where)
            ->field([
                'ol.*',
                'a.name',
            ])
            ->order($order);
        if (isset($param['is_excel']) && !empty($param['is_excel'])){
            //点击导出
            $arytitle = array(
                array('id', 'ID', 'A', '10'),
                array('name', '管理员', 'B', '30'),
                array('remarks', '操作说明', 'C', '40'),
                array('careful', '操作路径', 'D', '40'),
                array('action', '操作方法', 'E', '40'),
                array('create_time', '操作时间', 'F', '40'),
            );
            $filename = "管理员操作记录" . date("Y-m-d", time());
            $base64Str = exportExcel($filename, $arytitle, $list_->select()->toArray()); //文件名，标题数组，数据数组
            return $this->ajax(1, "成功", [
                'base64Str'=>$base64Str,
                'name'=>$filename.".xlsx"
            ]);
        }
        $list_ = $list_->paginate($page_size)
            ->toArray();
        $list = $list_ ['data'];
        if (!empty($list)) {
            foreach ($list as $k => $v) {

            }
        }
        return $this->ajax(1, '成功', [
            'list' => $list,
            'total' => $list_['total'],
            'page_size' => $page_size
        ]);
    }
}
